import cgi
import os
import time
import datetime

from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app

def printProperty(self, propertyName, value):
    self.response.out.write('<property name="')
    self.response.out.write(propertyName)
    self.response.out.write('">')
    self.response.out.write(value)
    self.response.out.write('</property>\n')

def openEntity(self, kind, key):
    self.response.out.write('<entity ')
    printAttribute(self, 'kind', kind)
    if key:
        printAttribute(self, 'key', key)
    self.response.out.write('>\n')

def openEntity(self, params, values):
    self.response.out.write('<entity ')
    for i in range(len(params)):
        if values[i]:
            printAttribute(self, params[i], values[i]) 
    self.response.out.write('>\n')

def printAttribute(self, name, value):
    self.response.out.write(' ')
    self.response.out.write(name)
    self.response.out.write('="')
    self.response.out.write(value)
    self.response.out.write('"')
    self.response.out.write(' ')
    
def checkRequiredParams(self, params):
    for param in params:
        if not self.request.get(param):
            self.response.out.write('Required parameter [')
            self.response.out.write(param)
            self.response.out.write('] not found')
            
#def handleGlobalRequests(self):
    #operation = self.request.get("operation")
    #entity = self.request.get("entity")
    #if operation and entity:
    #    if operation == 'list':
    #    listObjects(entity, self)
    #else:
    #    self.response.out.write('entity name not found: ')
    #    self.response.out.write(entity)
            
def addMessage(message, self):
    self.response.out.write('<message>')
    self.response.out.write(message)
    self.response.out.write('</message>')

def startDroidGuideMessage(self):
    self.response.headers['Content-Type'] = 'text/xml'
    self.response.out.write('<droidguide-message datetime="%s' % datetime.datetime.now())
    self.response.out.write('">\n')
    #handleGlobalRequests(self)
    
def finishDroidGuideMessage(self):
    #printDateTime(self)
    self.response.out.write('</droidguide-message>')

def printDateTime(self):
    self.response.out.write('<datetime>\n')
    self.response.out.write(datetime.datetime.now())
    self.response.out.write('</datetime>')

def printObjectShort(self, object):
    self.response.out.write('<object kind ="')
    self.response.out.write(object.kind())
    self.response.out.write('" key="')
    self.response.out.write(object.key())
    self.response.out.write('"/>\n')
    #self.response.out.write('</object>\n')

def printObjectShortWithProperty(self, object, property, value):
    self.response.out.write('<object kind ="')
    self.response.out.write(object.kind())
    self.response.out.write('" key="')
    self.response.out.write(object.key())
    self.response.out.write('" ')
    self.response.out.write(property)
    self.response.out.write('="')
    self.response.out.write(value)
    self.response.out.write('"/>\n')

            